import { getLanguageConfig, getTemplateConfig } from '../config/template';

// 导入语言包
import languageJson from '../language/index.json';

// 导入配置中的语言
const languageConfigJson: { [key: string]: any } = getTemplateConfig().language || {};

const defaultLanguageKey = 'zh';

/**
 * @param {Arrary} languageKey
 */
const extractLanguage = (languageKey: string[]) => {
  const json: { [key: string]: any } = {};
  languageKey.map((item) => {
    json[item] = {};
  });
  Object.keys(languageJson).map((item) => {
    languageKey.map((subItem) => {
      json[subItem][item] = languageJson[item][subItem] || languageJson[item].en;
    });
  });
  if (languageConfigJson.en) {
    languageKey.map((item) => {
      const templateLanguage = languageConfigJson[item]
        || languageConfigJson[item.slice(0, 2)]
        || languageConfigJson.en
        || {};
      json[item] = { ...json[item], ...templateLanguage };
    });
  }
  return json;
};
const {
  zh: zhCN,
  'zh-Hant': zhHant,
  ar,
  en: enUS,
  ja,
  de,
  fr,
  es,
  it,
  bg,
  hr,
  cs,
  da,
  nl,
  el,
  hu,
  id,
  kk,
  ko,
  lo,
  ms,
  pl,
  pt,
  ro,
  sv,
  th,
  vi,
  ru,
} = extractLanguage([
  'zh',
  'zh-Hant',
  'ar',
  'en',
  'ja',
  'de',
  'fr',
  'es',
  'it',
  'bg',
  'hr',
  'cs',
  'da',
  'nl',
  'el',
  'hu',
  'id',
  'kk',
  'ko',
  'lo',
  'ms',
  'pl',
  'pt',
  'ro',
  'sv',
  'th',
  'vi',
  'ru',
]);
const getAllLanguage = () => {
  return {
    zh: zhCN,
    'zh-Hant': zhHant,
    ar,
    en: enUS,
    ja,
    de,
    fr,
    es,
    it,
    bg,
    hr,
    cs,
    da,
    nl,
    el,
    hu,
    id,
    kk,
    ko,
    lo,
    ms,
    pl,
    pt,
    ro,
    sv,
    th,
    vi,
    ru,
  };
};

// 获取当前支持的语言列表
const getLanguageList = () => {
  const language = getLanguageConfig();
  const hasHant = !!language.find(item => item.key === 'zh-Hant');
  const list = [
    {
      label: '中文(简体)',
      value: 'zh',
    },
    ...(hasHant
      ? [
        {
          label: '中文(繁体)',
          value: 'zh-Hant',
        },
      ]
      : []),
    {
      label: 'English',
      value: 'en',
    },
  ];
  language.forEach((item) => {
    switch (item.key) {
      case 'ja': {
        list.push({ label: '日本語', value: 'ja' });
        break;
      }
      case 'de': {
        list.push({ label: 'Deutsch', value: 'de' });
        break;
      }
      case 'fr': {
        list.push({ label: 'Français', value: 'fr' });
        break;
      }
      case 'es': {
        list.push({ label: 'Español', value: 'es' });
        break;
      }
      case 'it': {
        list.push({ label: 'Italiano', value: 'it' });
        break;
      }
      case 'ar': {
        list.push({ label: 'العربية', value: 'ar' });
        break;
      }
      case 'bg': {
        list.push({ label: 'български', value: 'bg' });
        break;
      }
      case 'hr': {
        list.push({ label: 'hrvatski', value: 'hr' });
        break;
      }
      case 'cs': {
        list.push({ label: 'Česky', value: 'cs' });
        break;
      }
      case 'da': {
        list.push({ label: 'Dansk', value: 'da' });
        break;
      }
      case 'nl': {
        list.push({ label: 'Nederlands', value: 'nl' });
        break;
      }
      case 'el': {
        list.push({ label: 'Ελληνικά', value: 'el' });
        break;
      }
      case 'hu': {
        list.push({ label: 'Magyar', value: 'hu' });
        break;
      }
      case 'id': {
        list.push({ label: 'Orang indonesia', value: 'id' });
        break;
      }
      case 'kk': {
        list.push({ label: 'Қазақша', value: 'kk' });
        break;
      }
      case 'ko': {
        list.push({ label: '한국어', value: 'ko' });
        break;
      }
      case 'lo': {
        list.push({ label: 'Laotian', value: 'lo' });
        break;
      }
      case 'ms': {
        list.push({ label: 'Melayu', value: 'ms' });
        break;
      }
      case 'pl': {
        list.push({ label: 'Polski', value: 'pl' });
        break;
      }
      case 'pt': {
        list.push({ label: 'Português', value: 'pt' });
        break;
      }
      case 'ro': {
        list.push({ label: 'Românesc', value: 'ro' });
        break;
      }
      case 'sv': {
        list.push({ label: 'Svenska', value: 'sv' });
        break;
      }
      case 'th': {
        list.push({ label: 'ไทย', value: 'th' });
        break;
      }
      case 'vi': {
        list.push({ label: 'Tiếng việt', value: 'vi' });
        break;
      }
      case 'ru': {
        list.push({ label: 'Pусский', value: 'ru' });
        break;
      }
      default: {
        break;
      }
    }
  });
  return list;
};

/**
 * 格式化传入的语言
 * 转换成本地支持的
 * @param {string} languageKey
 */
function formatLanguageKey(languageKey: string) {
  const list = getAllLanguage();
  const languageArr = languageKey.split('-');
  let activeLang: string = '';
  languageArr.map((item, index) => {
    let string = '';
    for (let i = 0; i < languageArr.length - index; i++) {
      string += `${languageArr[i]}-`;
    }
    // 去掉尾部的符号
    string = string.substring(0, string.length - 1);
    if (list[string] && !activeLang) {
      activeLang = string;
    }
  });
  return activeLang || defaultLanguageKey;
}

function getLanguageKey(key?: string) {
  const lKey = key || defaultLanguageKey;
  return formatLanguageKey(lKey);
}

export { getLanguageList, getAllLanguage, getLanguageKey };
